﻿using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Batman.Core;
using Batman.Jianbei.Forum.Core.ChatRoom;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Linq;

namespace Batman.Jianbei.Forum.Core.ChatRoom
{
    /// <summary>
    /// 聊天室实体
    /// </summary>
    public class TalkRoom : Entity<string>
    {
        public string Name { get; set; }

        public string CreateBy { get; set; }

        public DateTime CreateAt { get; set; }

        /// <summary>
        /// 是否解散
        /// </summary>
        public bool IsDismiss { get; set; }

        public TalkRoom()
        {
            CreateAt = DateTime.Now;
        }

        /// <summary>
        /// 当前房间是否可以加入
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public bool CanJoin(IRepository<ChatAction, string> db)
        {
            var bf = Builders<ChatAction>.Filter.Where(t => t.RoomId == this.Id && t.Action != ChatActionEnum.离开房间);
            var groupBson = new BsonDocument
            {
                { "_id", new BsonDocument {{ "Uid","$Uid" }}},
                { "Count", new BsonDocument{{"$sum", 1 }}}
            };
            var query = db.GetQuery().Where(t => t.RoomId == this.Id && t.Action != ChatActionEnum.离开房间);
            var total = query.GroupBy(t => t.Uid).Count();

            return total < 30; // 限制房间人数30
        }
    }
}
